package demo;

import java.util.Date;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-08-02
 * Time: 14:56
 */
public class Test1 {
    static int sum1 = 0;
    static int sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        int[] arr = new int[10000000];
        long currentTime = new Date().getTime();
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i]  = random.nextInt(100)+1;
        }
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < arr.length; i+=2) {
                sum1 += arr[i];
            }
            System.out.println("线程1的和:"+sum1);
        });
        t1.start();
        Thread t2 = new Thread(() -> {
            for (int i = 1; i < arr.length; i+=2) {
                sum2 += arr[i];
            }
            System.out.println("线程2的和:"+sum2);
        });
        t2.start();


        t1.join();
        t2.join();
        System.out.println("两个线程总和:"+(sum1+sum2));
    }
}
